package com.atguigu.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class CircleBreakerRibbonController {

//    @Autowired
//    private RestTemplate restTemplate;
//    @Value("${service-url.nacos-user-service}")
//    private String URL;
//
//
//    @GetMapping("/consumer/fallback/{id}")
//    @SentinelResource(value = "fallback",fallback = "handlerFallback",blockHandler = "blockError")
//    public CommonResult<Payment> fallback(@PathVariable Long id) {
//        CommonResult<Payment> result = restTemplate.getForObject(URL + "/paymentSQL/" + id, CommonResult.class, id);
//        if (id == 4) {
//            throw new IllegalArgumentException("IllegalArgumentException 非法参数异常");
//        } else if (result.getData() == null) {
//            throw new NullPointerException("NullPointerException 该id没有对应记录,空指针异常");
//        }
//        return result;
//    }
//
//    public CommonResult<Payment> handlerFallback(@PathVariable Long id,Throwable e) {
//        Payment payment = new Payment(id, null);
//        return new CommonResult<>(444,"兜底异常handlerFallback",payment);
//    }
//
//    public CommonResult<Payment> blockError(@PathVariable Long id, BlockException e) {
//        Payment payment = new Payment(id, null);
//        return new CommonResult<>(450,"sentinel限流",payment);
//    }
}
